package com.ruochen.recipe.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.aries.ui.view.radius.RadiusTextView;
import com.blankj.utilcode.util.SizeUtils;
import com.ruochen.recipe.R;

public class QuantityControlView extends LinearLayout {

    private int numColor;
    private int numTextSize;
    private int numWidth;
    private int numHeight;
    private int initValue;
    private int maxValue;
    private int minValue;

    private int addNormalColor;
    private int addPressColor;
    private int addDisableColor;
    private int addWidth;
    private int addHeight;
    private String addText;
    private int addTextColor;
    private int addTextSize;

    private int subNormalColor;
    private int subPressColor;
    private int subDisableColor;
    private int subWidth;
    private int subHeight;
    private String subText;
    private int subTextColor;
    private int subTextSize;


    private TextView tvNum;
    private RadiusTextView rtvAdd;
    private RadiusTextView rtvSub;
    private OnNumChangeListener onNumChangeListener;

    public QuantityControlView(Context context) {
        this(context, null);
    }

    public QuantityControlView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public QuantityControlView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public QuantityControlView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initAttrs(context, attrs);
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        numColor = getResources().getColor(android.R.color.black);
        numTextSize = SizeUtils.sp2px(15);
        numWidth = SizeUtils.dp2px(50);
        numHeight = SizeUtils.dp2px(20);
        minValue = 0;
        maxValue = 9999;
        initValue = minValue;

        addWidth = SizeUtils.dp2px(20);
        addHeight = SizeUtils.dp2px(20);
        addText = "＋";
        addTextColor = getResources().getColor(android.R.color.white);
        addTextSize = SizeUtils.sp2px(15);
        addNormalColor = getResources().getColor(R.color.colorAppNormal);
        addPressColor = getResources().getColor(R.color.colorAppDeep);
        addDisableColor = getResources().getColor(R.color.colorAppPress);

        subNormalColor = addNormalColor;
        subPressColor = addPressColor;
        subDisableColor = addDisableColor;

        subWidth = SizeUtils.dp2px(20);
        subHeight = SizeUtils.dp2px(20);
        subText = "－";
        subTextColor = getResources().getColor(android.R.color.white);
        subTextSize = SizeUtils.sp2px(15);
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.QuantityControlView);
            numColor = a.getColor(R.styleable.QuantityControlView_qcv_numColor, numColor);
            numTextSize = a.getDimensionPixelSize(R.styleable.QuantityControlView_qcv_numTextSize, numTextSize);
            numWidth = a.getDimensionPixelSize(R.styleable.QuantityControlView_qcv_numWidth, numWidth);
            numHeight = a.getDimensionPixelSize(R.styleable.QuantityControlView_qcv_numHeight, numHeight);
            minValue = a.getInt(R.styleable.QuantityControlView_qcv_minValue, minValue);
            maxValue = a.getInt(R.styleable.QuantityControlView_qcv_maxValue, maxValue);
            initValue = a.getInt(R.styleable.QuantityControlView_qcv_initValue, initValue);

            addNormalColor = a.getColor(R.styleable.QuantityControlView_qcv_addNormalColor, addNormalColor);
            addPressColor = a.getColor(R.styleable.QuantityControlView_qcv_addPressColor, addPressColor);
            addDisableColor = a.getColor(R.styleable.QuantityControlView_qcv_addDisableColor, addDisableColor);
            addWidth = a.getDimensionPixelSize(R.styleable.QuantityControlView_qcv_addWidth, addWidth);
            addHeight = a.getDimensionPixelSize(R.styleable.QuantityControlView_qcv_addHeight, addHeight);
            addTextColor = a.getColor(R.styleable.QuantityControlView_qcv_addTextColor, addTextColor);
            addTextSize = a.getDimensionPixelSize(R.styleable.QuantityControlView_qcv_addTextSize, addTextSize);
            String mAddText = a.getString(R.styleable.QuantityControlView_qcv_addText);
            if (mAddText != null) {
                addText = mAddText;
            }

            subNormalColor = a.getColor(R.styleable.QuantityControlView_qcv_subNormalColor, subNormalColor);
            subPressColor = a.getColor(R.styleable.QuantityControlView_qcv_subPressColor, subPressColor);
            subDisableColor = a.getColor(R.styleable.QuantityControlView_qcv_subDisableColor, subDisableColor);
            subWidth = a.getDimensionPixelSize(R.styleable.QuantityControlView_qcv_subWidth, subWidth);
            subHeight = a.getDimensionPixelSize(R.styleable.QuantityControlView_qcv_subHeight, subHeight);
            subTextColor = a.getColor(R.styleable.QuantityControlView_qcv_subTextColor, subTextColor);
            subTextSize = a.getDimensionPixelSize(R.styleable.QuantityControlView_qcv_subTextSize, subTextSize);
            String mSubText = a.getString(R.styleable.QuantityControlView_qcv_subText);
            if (mSubText != null) {
                subText = mSubText;
            }
            a.recycle();
        }

        initWidget(context);
    }

    protected void onFinishInflate() {
        super.onFinishInflate();
        addListener();

    }

    public void initWidget(Context context) {
        LayoutInflater.from(context).inflate(R.layout.layout_customer_quantity_control, this);
        rtvAdd = findViewById(R.id.rtvAdd);
        rtvSub = findViewById(R.id.rtvSub);
        tvNum = findViewById(R.id.tvNum);

        tvNum.setTextColor(numColor);
        tvNum.setTextSize(numTextSize);
        tvNum.setText(String.valueOf(initValue));

        rtvAdd.getDelegate().setBackgroundColor(addNormalColor)
                .setBackgroundPressedColor(addPressColor)
                .setBackgroundDisabledColor(addDisableColor)
                .setTextColor(addTextColor)
                .init();
        rtvAdd.setTextSize(addTextSize);
        rtvAdd.setText(addText);

        rtvSub.getDelegate().setBackgroundColor(subNormalColor)
                .setBackgroundPressedColor(subPressColor)
                .setBackgroundDisabledColor(subDisableColor)
                .setTextColor(subTextColor)
                .init();
        rtvSub.setTextSize(subTextSize);
        rtvSub.setText(subText);

        LayoutParams lp = (LayoutParams) tvNum.getLayoutParams();
        lp.width = numWidth;
//        lp.height = numHeight;
        tvNum.setLayoutParams(lp);
        lp = (LayoutParams) rtvAdd.getLayoutParams();
        lp.width = addWidth;
//        lp.height = addHeight;
        rtvAdd.setLayoutParams(lp);
        lp = (LayoutParams) rtvSub.getLayoutParams();
        lp.width = subWidth;
//        lp.height = subHeight;
        rtvSub.setLayoutParams(lp);

    }

    public void addListener() {
        rtvAdd.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                int num = Integer.parseInt(tvNum.getText().toString());
                num++;
                if (num >= maxValue + 1)
                    return;
                tvNum.setText(String.valueOf(num));
                if (onNumChangeListener != null) {
                    onNumChangeListener.onChange(num, true);
                }
            }
        });

        rtvSub.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                int num = Integer.parseInt(tvNum.getText().toString());
                num--;
                if (num <= minValue - 1)
                    return;
                tvNum.setText(String.valueOf(num));
                if (onNumChangeListener != null) {
                    onNumChangeListener.onChange(num, false);
                }
            }
        });
    }

    public void setOnNumChangeListener(OnNumChangeListener onNumChangeListener) {
        this.onNumChangeListener = onNumChangeListener;
    }

    public interface OnNumChangeListener {
        void onChange(int num, boolean isAdd);
    }


}
